package f.v4;

import v4.text.TextJm;
import v4.text.TextJmConfig;

public class TextZjm {
	
	static final String TAG = "zjm=";
	static final String SPT = "-";
	private TextJm textJm;
	
	public TextZjm(TextJm textJm) {
		this.textJm = textJm;
	}
	
	private static TextZjm zjm = null;
	public static TextZjm defaultZjm(TextJmConfig jmConfig) {
		if (zjm == null) {
			if (jmConfig == null) {
				zjm = new TextZjm(new TextJm(new TextJmConfig()));
			} else {
				zjm = new TextZjm(new TextJm(TextJmConfig.copy(jmConfig)));
			}
		}
		return zjm;
	}
	
	public String jm(String str) {
		if(str.startsWith(TAG)) {
			int i = str.indexOf(SPT);
			String etext = str.substring(i+1);
			String k = str.substring(TAG.length(), i);
			return textJm.decrypt(etext, k);
		}else {
			String k = TextJmConfig.r(6);
			String etext = textJm.encryptAndCheck(str, k);
			return TAG + k + SPT + etext;
		}
	}
	
	public static void main(String[] args) {
		String str = defaultZjm(null).jm(args[0]);
		System.out.println(str);
	}

}
